<?php



function webservice( $Action , $Module , $GET = NULL , $POST = NULL){
	global $_GLOBAL;
	$ServerUrl = C('API_DOMAIN');
	
	$CLIENT_ID = C('CLIENT_ID');
	if(!empty($CLIENT_ID)){
	    $hash = md5(date('Y').C('WX_APP_ID').$CLIENT_ID);
	    $url = '/client/'.$CLIENT_ID.'/hash/'.$hash.'/';
	}
	
	$get_string = '';
	$get_string_record = '';
	if(is_array($GET)){
		foreach($GET as $key=>$value){
			if(isset($key) && isset($value) && trim($value)!=''){
				$get_string .= "/$key/".urlencode($value);
			}
		}
		$get_string_record = $get_string;
		$a = debug_backtrace();
// 		@$get_string .= "/invokefrom/".str_replace(array('\\','/'),"_",$a[0]['file']).'_at_line_'.$a[0]['line']."_use_in_".$a[1]['function']."_from_".$_SERVER['REMOTE_ADDR']."_addr_".$_SERVER['HTTP_HOST']."_addrx_".$_SERVER['HTTP_X_FORWARDED_HOST'];
	}elseif(empty($get_string)){
		$get_string = $GET;
		$a = debug_backtrace();
// 		@$get_string .= "/invokefrom/".str_replace(array('\\','/'),"_",$a[0]['file']).'_at_line_'.$a[0]['line']."_use_in_".$a[1]['function']."_from_".$_SERVER['REMOTE_ADDR']."_addr_".$_SERVER['HTTP_HOST']."_addrx_".$_SERVER['HTTP_X_FORWARDED_HOST'];
	}
	
	$str_url = $ServerUrl . "/$Action/$Module/".$url.$get_string;
	if(isset($POST['target']) && substr($POST['target'],0,1)=='/'){
		$POST['target'] = $ServerUrl.$POST['target'];
	}
	$_GLOBAL['WebService_getLastUrl'] = $str_url;
	$_GLOBAL['WebService_getLastPost'] = $POST;
	
	
	
	$ch_curl = curl_init ();
	curl_setopt ( $ch_curl, CURLOPT_TIMEOUT, 20);//3s
	curl_setopt ( $ch_curl, CURLOPT_HEADER, false );
	if(is_array($POST)){
		curl_setopt ( $ch_curl, CURLOPT_POST , 1);
		if(substr(@$POST['file'],0,1) =='@'){
			@curl_setopt ( $ch_curl, CURLOPT_POSTFIELDS , ($POST));
		}else{
			curl_setopt ( $ch_curl, CURLOPT_POSTFIELDS , http_build_query($POST));
		}
		
	}else{
		curl_setopt ( $ch_curl, CURLOPT_POST , 0);
	}
	
	curl_setopt ( $ch_curl, CURLOPT_RETURNTRANSFER, true );
	curl_setopt ( $ch_curl, CURLOPT_URL, $str_url );
	$str_return = curl_exec ( $ch_curl );
	
	
	$info = json_decode($str_return);
	$head = curl_getinfo($ch_curl);
	
	Log::write($str_url.print_r($POST,true),Log::INFO);
	
	$return = unserialize($info);
	
	return $return ;
}